﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GeoPal
{
    /// <summary>
    /// Application logic of the formStatistics form
    /// </summary>
    public partial class formStatistics : Form
    {
        /// <summary>
        /// Current GPS hangle
        /// </summary>
        public Gps gpsData;
        /// <summary>
        /// Current Communicator object
        /// </summary>
        public Communicator commData;
        /// <summary>
        /// Timer for the periodic refreshing
        /// </summary>
        private Timer gpsTimer;
        /// <summary>
        /// Delegate to add log element
        /// </summary>
        /// <param name="message">Element text</param>
        public delegate void UpdateLogDelegate(string message);
        UpdateLogDelegate updateLogDelegate;
        /// <summary>
        /// Constructor of the formStatistics class
        /// </summary>
        /// <param name="gps">GPS handle</param>
        /// <param name="communicator">Communicator class</param>
        public formStatistics(ref Gps gps, ref Communicator communicator)
        {
            InitializeComponent();
            gpsData = gps;
            commData = communicator;
            gpsTimer = new Timer();
            gpsTimer.Interval = 1000;
            gpsTimer.Enabled = true;
            gpsTimer.Tick += new System.EventHandler(gpsTimer_Tick);
            updateLogDelegate = new UpdateLogDelegate(UpdateLog);
        }
        /// <summary>
        /// Updates object references according to the given parameters
        /// </summary>
        /// <param name="gps">GPS handle</param>
        /// <param name="communicator">Communicator class</param>
        public void updateObjects(ref Gps gps, ref Communicator communicator)
        {
            gpsData = gps;
            commData = communicator;
        }
        /// <summary>
        /// Adds new item to the log (indirectly)
        /// </summary>
        /// <param name="text">Item text</param>
        public void AddLogString(string text)
        {
            tbLog.Invoke(updateLogDelegate, new object[] { text });
        }
        /// <summary>
        /// Adds new item to the log and scrolls down the list
        /// </summary>
        /// <param name="text">Item text</param>
        public void UpdateLog(string text)
        {
            tbLog.Text += DateTime.Now.ToLongTimeString() + ": " + text + "\r\n";
            tbLog.SelectionStart = tbLog.Text.Length;
            tbLog.ScrollToCaret();
        }
        /// <summary>
        /// Timer handle
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event arguments</param>
        private void gpsTimer_Tick(object sender, EventArgs e)
        {
            refreshData();
        }
        /// <summary>
        /// Refreshes data fields
        /// </summary>
        private void refreshData()
        {
            if (gpsData != null)
            {
                labelLat.Text = "Lat: ";
                if (gpsData.position.latitude != 0)
                    labelLat.Text += gpsData.position.latitude.ToString();
                else
                    labelLat.Text += "-";

                labelLong.Text = "Long: ";
                if (gpsData.position.longitude != 0)
                    labelLong.Text += gpsData.position.longitude.ToString();
                else
                    labelLong.Text += "-";

                labelTime.Text = "Time: ";
                if (gpsData.position.time != null)
                    labelTime.Text += gpsData.position.time.ToString();
                else
                    labelTime.Text += "-";

                labelSpeed.Text = "Speed: ";
                if (gpsData.position.speed > -1)
                    labelSpeed.Text += gpsData.position.speed.ToString();
                else
                    labelSpeed.Text += "-";

                labelSatellites.Text = "Satellites: ";
                if (gpsData.satellitesInView > -1)
                    labelSatellites.Text += gpsData.satellitesInView.ToString();
                else
                    labelSatellites.Text += "-";
            }
            if (commData != null)
            {
                if (commData.ReceivedBytes < 1024)
                {
                    labelReceivedBytes.Text = "Received data: " + commData.ReceivedBytes + " B";
                }
                else
                {
                    labelReceivedBytes.Text = "Received data: " + (commData.ReceivedBytes / 1024.0).ToString("F3") + " kB";
                }
                if (commData.SendBytes < 1024)
                {
                    labelSentBytes.Text = "Sent data: " + commData.SendBytes + " B";
                }
                else
                {
                    labelSentBytes.Text = "Sent data: " + (commData.SendBytes / 1024.0).ToString("F3") + "kB";
                }
            }

        }
        /// <summary>
        /// Closes the form
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event arguments</param>
        private void menuItem1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// Clears the log
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event arguments</param>
        private void btClear_Click(object sender, EventArgs e)
        {
            tbLog.Text = "";
        }
    }
}